<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="开始年龄" prop="ageStart">
        <el-input v-model="dataForm.ageStart" placeholder="开始年龄"></el-input>
      </el-form-item>
      <el-form-item label="结束年龄" prop="ageEnd">
        <el-input v-model="dataForm.ageEnd" placeholder="结束年龄"></el-input>
      </el-form-item>
      <el-form-item label="眼轴标准" prop="alStandard">
        <el-input v-model="dataForm.alStandard" placeholder="眼轴标准"></el-input>
      </el-form-item>
      <el-form-item label="蓝色预警" prop="alBlue">
        <el-input v-model="dataForm.alBlue" placeholder="蓝色预警"></el-input>
      </el-form-item>
      <el-form-item label="黄色预警" prop="alYellow">
        <el-input v-model="dataForm.alYellow" placeholder="黄色预警"></el-input>
      </el-form-item>
      <el-form-item label="红色预警" prop="alRed">
        <el-input v-model="dataForm.alRed" placeholder="红色预警"></el-input>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-prevent-re-click>确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { getObj, addObj, putObj } from '@/api/admin/dictAl'

  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          id: 0,
          ageStart: '',
          ageEnd: '',
          alStandard: '',
          alBlue: '',
          alYellow: '',
          alRed: '',
          createTime: '',
          updateTime: '',
          createUser: '',
          modifyUser: '',
          tenantId: '',
          delFlag: ''
        },
        dataRule: {
          ageStart: [{
            required: true,
            message: '开始年龄不能为空',
            trigger: 'blur'
          }],
          ageEnd: [{
            required: true,
            message: '结束年龄不能为空',
            trigger: 'blur'
          }],
          alStandard: [{
            required: true,
            message: '眼轴标准不能为空',
            trigger: 'blur'
          }],
          alBlue: [{
            required: true,
            message: '蓝色预警不能为空',
            trigger: 'blur'
          }],
          alYellow: [{
            required: true,
            message: '黄色预警不能为空',
            trigger: 'blur'
          }],
          alRed: [{
            required: true,
            message: '红色预警不能为空',
            trigger: 'blur'
          }]

        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
              this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putObj(this.dataForm).then(data => {
                this.$message.success('修改成功')
                this.visible = false
                this.$emit('refreshDataList')
              });
            } else {
              addObj(this.dataForm).then(data => {
                this.$message.success('添加成功')
                this.visible = false
                this.$emit('refreshDataList')
              })
            }
          }
        })
      }
    }
  }
</script>
